import Vue from 'vue';
import Vuex from 'vuex';
import { removeUserCookie, getUserCookie, setUserCookie } from '@/utils/userCookie';

Vue.use(Vuex);

export default new Vuex.Store({
  state: {
    collapsed: false,
    userInfo: getUserCookie(),
    router: [],
  },
  mutations: {
    changCollapsed(state) {
      state.collapsed = !state.collapsed;
    },
    setUserInfo(state, userInfo) {
      state.userInfo = userInfo;
    },
    removeUserInfo(state) {
      state.userInfo = {
        username: '',
        appkey: '',
        role: '',
        email: '',
      };
      removeUserCookie();
    },
    changRouter(state, router) {
      state.router = router;
    },
  },
  actions: {
    changCollapsed({ commit }) {
      commit('changCollapsed');
    },
    setUserInfo({ commit }, userInfo) {
      commit('setUserInfo', userInfo);
      setUserCookie(userInfo);
    },
    removeUserInfo({ commit }) {
      commit('removeUserInfo');
    },
    changRouter({ commit }, router) {
      commit('changRouter', router);
    },
  },
  modules: {},
});
